﻿# StartTCL: n
#
# PvP Duell Script v1.0
#
# Credits to:
# Rama for the Script
# Hiperblade for Addon
#
#
namespace eval ::zDuelSystem {

	###############################################################
	# SETTINGS:
	#
	# Set here how much time for dmg after one loosed...
	variable NO_DMG_TIME 5
	#
	# Set here the emoteid if player loosed...
	variable EMOTE_LOOSE 20
	#
	# Set here the emoteid if player wined...
	variable EMOTE_WIN 15
	#
	# Set here the Dueling dir...
	variable DUELING_DIR "scripts/extra/dueling"
	#
	#
	###############################################################
	# Languages:
	#
	#
	# English:
	::Texts::Set en end_dueling "/duelSystemEvent %s has defeated %s in a duel!"
	#
	# German:
	::Texts::Set de end_dueling "/duelSystemEvent %s hat %s in einem Duell besiegt!"
	#
	#
	###############################################################
	variable NAME "zDuelSystem"
	variable VERSION 1.0
	package require "SQLdb" 0.2.4
	::StartTCL::Require "StartTCL" 0.9.5
	if { ![ file exists "$DUELING_DIR" ] } { file mkdir "$DUELING_DIR" }
}

proc ::zDuelSystem::Cmd { player cargs } {
set cargs [ split $cargs ]
	switch $cargs {

		"check_selected" {
			set Selected [::GetSelection $player]
				if { [ ::GetObjectType $Selected ] != 4 || [::Distance $player $Selected] > 15  } { return "/duelSystemEvent INVITE_ERROR [::GetName $player] [::GetName $Selected]" }
			#
			set file [open "$::zDuelSystem::DUELING_DIR/$player.d" w+]; puts $file "$Selected"; close $file
			set file [open "$::zDuelSystem::DUELING_DIR/$Selected.d" w+]; puts $file "$player"; close $file
			#
			return "/duelSystemEvent INVITE_SEND [::GetName $player] [::GetName $Selected]"
		}

		"decline" {
		#
		set hfile [open "$::zDuelSystem::DUELING_DIR/$player.d" r+]; set otherplayerid [gets $hfile]; close $hfile
		file delete "$::zDuelSystem::DUELING_DIR/$player.d"
		file delete "$::zDuelSystem::DUELING_DIR/$otherplayerid.d"
		#
		return "/duelSystemEvent INVITE_DECLINE [::GetName $player] [::GetName $otherplayerid]"
		}

		"accept" {
		#
		set hfile [open "$::zDuelSystem::DUELING_DIR/$player.d" r+]; set otherplayerid [gets $hfile]; close $hfile
		#
			if { [::Custom::GetPlayerSide $otherplayerid] } {; ::CastSpell $otherplayerid $otherplayerid 23333
			} else {; ::CastSpell $otherplayerid $otherplayerid 23335 }
				if { [::Custom::GetPlayerSide $player] } {; ::CastSpell $player $player 23333
			} else {; ::CastSpell $player $player 23335 }
			return "/duelSystemEvent START_TIMER [::GetName $player] [::GetName $otherplayerid]"
		}

		"end_duel" {
		#
		set hfile [open "$::zDuelSystem::DUELING_DIR/$player.d" r+]; set otherplayerid [gets $hfile]; close $hfile
		#
			if { [::Custom::GetPlayerSide $otherplayerid] } {; ::CastSpell $otherplayerid $otherplayerid 23334
			} else {; ::CastSpell $otherplayerid $otherplayerid 23336 }
				if { [::Custom::GetPlayerSide $player] } {; ::CastSpell $player $player 23334
			} else {; ::CastSpell $player $player 23336 }

			set nodmgtime [ expr { [clock seconds] + $::zDuelSystem::NO_DMG_TIME } ]
			
			set file [open "$::zDuelSystem::DUELING_DIR/$player.h" w+]; puts $file "$nodmgtime"; close $file
			set file [open "$::zDuelSystem::DUELING_DIR/$otherplayerid.h" w+]; puts $file "$nodmgtime"; close $file
			file delete "$::zDuelSystem::DUELING_DIR/$player.d"
			file delete "$::zDuelSystem::DUELING_DIR/$otherplayerid.d"
			
			::Emote "$player" "$otherplayerid" "$::zDuelSystem::EMOTE_LOOSE"
			::Emote "$otherplayerid" "$player" "$::zDuelSystem::EMOTE_WIN"
			return [::Texts::Get end_dueling [::GetName $otherplayerid] [::GetName $player]]
		}
	}
}

::Custom::HookProc "::WoWEmu::DamageReduction" {
		if { [ file exists "$::zDuelSystem::DUELING_DIR/$player.h" ] } {
		set hfile [open "$::zDuelSystem::DUELING_DIR/$player.h" r+]; set time [gets $hfile]; close $hfile
			if { [clock seconds] < $time } { return 1.0
			} else { file delete "$::zDuelSystem::DUELING_DIR/$player.h"
		}
	}
}

::Custom::AddCommand "dueling_cmd" "::zDuelSystem::Cmd"
